/**
 * area模块接口
 */
const AV = require("leanengine");
const BaseService = require("./baseService");
class Service extends BaseService {
  constructor() {
    super("Area", "地区");
  }

  // 覆盖父类的方法
  addInterface(obj) {
    const entry = new AV.Object("Area");
    // 添加对象里的所有属性
    for (let p in obj) {
      entry.set(p, obj[p]);
    }
    let { cityId } = obj;
    // 存在cityId,说明城市已存在
    let City = AV.Object.createWithoutData("City", cityId);
    return entry.save();
  }

  async findByCityId(ctx) {
    const { cityId } = ctx.params;
    if (!cityId) {
      return {
        code: 601,
        msg: "请输入cityId"
      };
    }
    try {
      const query = new AV.Query("Area");
      query.equalTo("cityId", cityId);
      const result = await query.find();
      const areas = result.map(item => {
        return {
          ...item.attributes,
          areaId: item.id
        };
      });
      return {
        code: "666",
        msg: "success",
        areas
      };
    } catch (error) {
      return {
        code: 500,
        msg: "查询失败",
        error
      };
    }
  }

  async edit(ctx) {
    const { params } = ctx;
    let result = this.checkAttr(
      ["cityId", "cityName", "name", "areaId"],
      params
    );
    if (result) return result;
    const entry = AV.Object.createWithoutData("Area", params.areaId);
    for (let p in params) {
      // id不能设置
      if (p.indexOf("Id") === -1) {
        entry.set(p, params[p]);
      }
    }
    try {
      result = await entry.save();
      return {
        code: "666",
        msg: "编辑成功",
        result
      };
    } catch (error) {
      console.log(error);
      return {
        ...this.errInfo(error)
      };
    }
  }

  async add(ctx) {
    const { params } = ctx;
    let { name } = params;
    let result = this.checkAttr(["cityId", "cityName", "name"], params);
    if (result) return result;
    const entry = new AV.Object("Area");
    for (let p in params) {
      entry.set(p, params[p]);
    }
    try {
      result = await this.checkName(name);
      if (result) return result;
      result = await entry.save();
      return {
        code: "666",
        msg: "添加成功"
      };
    } catch (error) {
      return {
        ...this.errInfo(error)
      };
    }
  }

  async delById(ctx) {
    let { areaId } = ctx.params;
    if (!areaId) {
      return {
        code: "604",
        msg: "缺少areaId"
      };
    }
    try {
      const result = await this.delInterfaceById(areaId);
      return {
        code: "666",
        msg: "删除成功"
      };
    } catch (error) {
      return {
        ...this.errInfo(error)
      };
    }
  }

  async findById(ctx) {
    const { areaId } = ctx.params;
    let result = this.checkAttr(["areaId"], ctx.params);
    if (result) return result;
    let query = new AV.Query("Area", areaId);
    try {
      result = await query.get(areaId);
      let area = {
        ...result.attributes,
        areaId
      };
      return {
        code: "666",
        msg: "success",
        area
      };
    } catch (error) {
      return {
        ...this.errInfo(error)
      };
    }
  }

  async allArea(ctx) {
    try {
      const query = new AV.Query(this.moduleName);
      let list = await query.find();
      let areas = list.map(item => {
        return {
          ...item.attributes,
          areaId: item.id
        };
      });
      return {
        code: "666",
        msg: "success",
        areas
      };
    } catch (error) {
      return {
        code: error.code,
        msg: error.message
      };
    }
  }

  async getList(ctx) {
    const { pageNum = 1, pageSize = 10 } = ctx.params;
    try {
      const query = new AV.Query(this.moduleName);
      query.limit(pageSize);
      query.skip(pageNum * pageSize - pageSize);
      // 升序
      // query.ascending('createdAt');
      // 降序
      query.descending("createdAt");
      const total = await query.count();
      let list = await query.find();
      let areas = list.map(item => {
        return {
          ...item.attributes,
          areaId: item.id
        };
      });
      return {
        code: "666",
        msg: "success",
        pageNum,
        pageSize,
        total,
        areas
      };
    } catch (error) {
      console.log(error);
      return {
        code: error.code,
        msg: error.message
      };
    }
  }
}

const service = new Service();
module.exports = service;
